package com.navi.loaddata.job;


import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.job.QuartzManage;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.mysqlEntity.EtlConfDMapper;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.core.util.ProcessUtil;
import com.navi.loaddata.loadService.IEtlConfServices;
import com.navi.loaddata.loadService.IEtlConfServices2;
import com.navi.loaddata.loadService.impl.EqEvent.LoadEqStateService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jay
 */
@Service
//@DisallowConcurrentExecution//禁止并发
public class LoadEqState implements Job {
    private static final Logger logger = LoggerFactory.getLogger(LoadEqState.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private QuartzManage quartzManage;

    @Autowired
    private LoadEqStateService loadEqStateService;

    @Autowired
    private IEtlConfServices2 etlConfServices;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //get JOB
        EtlConfD etlConfD = etlConfServices.getNewConf(context);
        if (null == etlConfD) return;

        etlConfD = etlConfServices.beginJob(etlConfD);
        if (StringUtils.isEmpty(etlConfD.getQueryMinute())) return;
        startETL(etlConfD);
    }

    private void startETL(EtlConfD etlConfD) {
        //获取Eqpt
        List<String> eqpts = etlConfServices.getEqptsList(etlConfD);
        if(eqpts.isEmpty()) eqpts=loadEqStateService.getEqpts(etlConfD);
        if(eqpts.isEmpty()) return;//DISTINCT 时发生 EXCEPTION
        if (null==eqpts.get(0)){//没有数据
            String[] resMsg ={"0","0",null};//okCnt,errCnt,errMsg
            EtlConfDModel etlConfDByOpe = etlConfServices.endJobOfModel(etlConfD);
            etlConfServices.addHisOfModel(etlConfDByOpe, resMsg);
            etlConfServices.endJob(etlConfD);
            return;
        }

        EtlConfDModel etlConfDModel= EtlConfDMapper.INSTANCE.etlConfDToModel(etlConfD);
        //循环Eqpt
        for(String eqpt:eqpts){
            etlConfDModel.setEqpt(eqpt);
            String[] resMsg=new String[3];//okCnt,errCnt,errMsg
            try {
                resMsg = loadEqStateService.loadEqStateDataMain(etlConfDModel);

                EtlConfDModel etlConfDByEqpt= etlConfServices.endJobOfModel(etlConfD);
                if (null == etlConfDByEqpt) return;
                etlConfDByEqpt.setEqpt(eqpt);
                etlConfServices.addHisOfModel(etlConfDByEqpt, resMsg);
            }catch (ETLException2 e) {
                String err=e.getErrMsg();
                //print Log
                logger.error("Job {} [{}]-[{}] EQPT={} FAIL TO SYNCHRONIZE.\n{}\n{}",
                        etlConfD.getProjectName() + "_" + etlConfD.getJobGroupName() + "_" + etlConfD.getJobName(),
                        etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp(), eqpt,
                        e.getErrorCode().getErrorMsg(),LoggerUtil.stackTraceToString(e)
                );
                EtlConfDModel etlConfDByEqpt= etlConfServices.endJobOfModel(etlConfD);
                if (null == etlConfDByEqpt) return;
                etlConfDByEqpt.setEqpt(eqpt);
                resMsg[0] = "0";
                resMsg[1] = "-1";
                resMsg[2] = "eqpt=" + eqpt + ": " + err;
                etlConfServices.addHisOfModel(etlConfDByEqpt, resMsg);

                if (99999 == e.getErrorCode().getErrorCode()) {//无法连接,SLEEP
                    etlConfServices.resetJob(etlConfD);
                    return;
                }
            } catch (Exception e) {
                String err = LoggerUtil.stackTraceToString(e);
                logger.error("Job {} [{}]-[{}] EQPT={} FAIL TO SYNCHRONIZE.\n{}",
                        etlConfD.getProjectName() + "_" + etlConfD.getJobGroupName() + "_" + etlConfD.getJobName(),
                        etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp(), eqpt,
                        LoggerUtil.stackTraceToString(e)
                );
                EtlConfDModel etlConfDByEqpt= etlConfServices.endJobOfModel(etlConfD);
                if (null == etlConfDByEqpt) return;
                etlConfDByEqpt.setEqpt(eqpt);
                resMsg[0] = "0";
                resMsg[1] = "-1";
                resMsg[2] = "eqpt=" + eqpt + ": " + err;
                etlConfServices.addHisOfModel(etlConfDByEqpt, resMsg);

                if (null != e && "Y".equals(etlConfServices.specialErr(etlConfDByEqpt.getJobName(),err))) {//无法连接,SLEEP
                    etlConfServices.resetJob(etlConfD);
                    return;
                }
            }
        }
        etlConfServices.endJob(etlConfD);

    }
}
